﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Threading;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.FileMaintenance;
using DragDropLibrary;
using ImageAppDataModels;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderOperationHandler
    {    
        public virtual void DeleteCheckedUnderFolderBackground(FolderViewModel f)
        {
            FolderViewModel root = f.FindRoot();
            root.AddOperation("DeleteChecked");
            ParameterizedThreadStart ts = new ParameterizedThreadStart(DeleteCheckedWorker);
            Thread t = new Thread(ts) { IsBackground = true, Priority = ThreadPriority.BelowNormal };
            t.Start(f);
        }

        public virtual void DeleteCheckedUnderFolder(FolderViewModel f)
        {            
            f.DoRecursive((folder) =>
            {
                if (folder.IsChecked)
                {
                    if (Directory.Exists(folder.FullPath))
                    {
                        try
                        {
                            DirectoryInfo di = new DirectoryInfo(folder.FullPath);
                            FileWorker.SetFileAttribute(di, FileAttributes.Normal);
                            Directory.Delete(folder.FullPath, true);
                        }
                        catch
                        {
                            // nothing
                        }
                    }
                }
            });
            FolderExplorerControl._Dispatcher.BeginInvoke(new VoidDelegate(() => f.Parent.RefreshSubfolders()));

            FolderViewModel root = f.FindRoot();
            root.RemoveOperation("DeleteChecked");            
        }

        private void DeleteCheckedWorker(object o)
        {
            DeleteCheckedUnderFolder(o as FolderViewModel);            
        }     
    }
}
